<?php
/*
 * Created on Dec 7, 2011
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

include_once("../configure/configure.php");		//--> global var
include_once("../lib/impl/KeywordImpl.php");
include_once("../admin/lib/utils/StringUtils.php");
include_once("../admin/lib/utils/CouponUtils.php");


set_time_limit(0);
header("Content-type:text/html; charset=utf-8");
   
updateCouponCode();
//test();

//echo "===" . parseCode("Chipkos Footwear - Chipkos Original's Women's - Black - Use Promo Code AHIMSA for 10% off.");

function parseCode($name)
{
	$startTag 	= 'code ';
	$pos 		= strripos($name, $startTag);
	
	$rule = 1;
	
	if(!$pos){
		$startTag 	= 'code:';
		$pos 		= strripos($name, $startTag);
		
		$rule 		= 2;
	}
	
	if($pos){
		$str 	= substr($name, $pos + strlen($startTag));
		
		$str	= trim($str);
		$str	= trim($str, '-');
		$str	= trim($str, '.');
		$str	= trim($str);
		
		$arr 	= explode(' ', $str);
		$code 	= trim($arr[0], '.');
		
		if($rule === 1 && StringUtils::isUpperNum($code) || $rule === 2 && StringUtils::isCharNum($code))
			return $code;
	}
	
	return '';
}

function findCode($name, $startTag)
{
	$pos 	= strripos($name, $startTag);
	
	if($pos){
		$str 	= substr($name, $pos + strlen($startTag));
		
		$str	= trim($str);
		$str	= trim($str, '-');
		$str	= trim($str, '.');
		$str	= trim($str);
		
		$arr 	= explode(' ', $str);
		$code 	= trim($arr[0], '.');
		
		$code 	= str_ireplace('Required', '', $code);
		$code 	= str_ireplace('needed', '', $code);
		
		$code	= trim($code, '?');
		$code	= trim($code, '!');
		$code	= trim($code, ',');
		$code	= trim($code, '"');
		$code	= trim($code, ')');
		$code	= trim($code, '(');
		
		if(StringUtils::isCouponCode($code))
			return $code;
	}
	
	return '';
}

function findCodes($name)
{
	$arrTags = array();
	$arrTags[]	= 'code ';
	$arrTags[]	= 'code:';
	$arrTags[]	= 'code?';
	$arrTags[]	= 'code.';
	$arrTags[]	= 'code=';
	$arrTags[]	= 'coupon :';
	$arrTags[]	= 'code :';
	
	foreach ($arrTags as $tag) {
		$code = findCode($name, $tag);
		
		if(strlen($code) < 3){
			continue;
		}else{
			return $code;
		}
	}
	
	return '';
}

function updateCouponCode()
{
	$counter	= 0;
	
	$oKeywordImpl 	= new KeywordImpl(DB_TAG_COUPON);
	
	
	$sql = "SELECT id, name FROM coupon WHERE NAME LIKE '%code%' AND CODE = '' ORDER BY id DESC LIMIT 100;";
	
	echo "sql = $sql \n";
	
	$allData = $oKeywordImpl->getAllData(DB_TAG_COUPON, $sql);
	
	echo '<br>waitting update calculate count = ' . count($allData) . '<br>';
	foreach($allData as $arrRow){
		$couponId 	= $arrRow['id'];
		$name		= $arrRow['name'];
		$code		= CouponUtils::findCode($name);
		
		echo "<br>***************************************************";
		
		echo "<br> id = $couponId";
		echo "<br> name = $name";
		
		if(!empty($code)){
			echo "<br> code ==== " . $code;
			
			$sql		= "UPDATE coupon SET code = '$code' WHERE id = $couponId";
			$counter 	+= $oKeywordImpl->update(DB_TAG_COUPON, $sql);
			
		}
			
	}
	
	echo "<br> counter = $counter";
}

?>
